package com.yunhe.fountain.scoket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @ClassName ClientLaunch
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/7 19:25
 * @Version 1.0
 **/
public class ClientLaunch {

//    private Socket socket = new Socket("47.108.95.221", 6126);
//
//    private SocketWriter socketWriter;
//
//    private InputStream socketReader;
//
//    private String strInput;

    public String Client(Socket socket, SocketWriter s, InputStream socketReader, String strInput) {
        String strOutput = "";
        try {//客户端输出流作为服务器的输入

            OutputStream socketWriter = socket.getOutputStream();

            socketWriter.write(s.hexStringToBytes(strInput));

            socketWriter.flush();
            Thread.sleep(100);
            socketReader = socket.getInputStream();
            int count = socketReader.available();
            int countFlag = 0;
            while(count == 0){
                count = socketReader.available();
                countFlag++;
                if(countFlag>10){
                    socket.shutdownOutput();
                    break;
                }
            }
            if(count !=0){
                byte[] temp = new byte[count];
                int bytes = 0;
                bytes = socketReader.read(temp);
                if (bytes != count) {
                    return null;
                }
                strOutput += ByteUtil.BinaryToHexString(temp);
//            byte[] array = new byte[2];
//            for (int i = 1; i < 3; i++) {
//                array[i - 1] = temp[i];
//
//            }
//            int let = ByteUtil.bytes2Short2(array);
//
//            array = new byte[let];
//
//            bytes = socketReader.read(array);
//            if (bytes != let) {
//                return null;
//
//            }
//
//            strOutput += ByteUtil.BinaryToHexString(array);
                //把字符串中“ ”去掉
                strOutput = strOutput.replaceAll(" ", "");
            }else {
                return null;
            }


        } catch (Exception e) {
            e.printStackTrace();

        }
        return strOutput;

    }


    public ClientLaunch() {
    }
}
